<?php

/**
 * 自动生成 WxEventUserService
 * 
 * Apps/Common/Service/WxEventUserService.class.php
 * Zix @ 2015-07-06
 */

namespace Common\Service;

class WxEventUserService {
  
  var $award_status = array(
    1 => '未查看',
    2 => '未兑换',
    3 => '已验证',
    4 => '已领取',
  );
  
  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'event_id' => '',
      'user_id' => '',
      'award_id' => '',
      'ipaddress' => '',
      'take_time' => '',
    );
  }

  function get_by_id( $id , $with_award = FALSE ) {
    $WxEventUser = M('WxEventUser');
    
    if ( $with_award ) {
      $data = $WxEventUser
        ->field('eu.* , award.name as award_name, award.rank as award_rank , award.password , award.valid_type')
        ->alias('eu')
        ->where('eu.id = %d' , $id )
        ->join('wx_event_award award on award.id = eu.award_id')
        ->find();
      //echo $WxEventUser->_sql();
    } else {
      $data = $WxEventUser->find( $id );
    }
    
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'mer_id' => '',
      'event_type' => '',
      'event_id' => '',
      'user_id' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    
    $WxEventUser = M('WxEventUser');

    $where = array();

    if (!empty($config['status'])) {
      $where['weu.status'] = $config['status'];
    }

    if (!empty($config['user_id'])) {
      $where['weu.user_id'] = $config['user_id'];
    }
    
    if (!empty($config['event_type'])) {
      $where['weu.event_type'] = $config['event_type'];
    }

    if (!empty($config['event_id'])) {
      $where['weu.event_id'] = $config['event_id'];
    }

    $join_table_name = 'wx_event_' . strtolower( $config['event_type'] );
    if ($config['count']) {
      return $WxEventUser->alias('weu')->where($where)->count();
    } else {
      $order = 'weu.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventUser
        ->field('weu.* , we.name , we.title , award.name as award_name , award.rank as award_rank, user.phone , user.username ')
        ->alias('weu')
        ->join($join_table_name . ' we on we.id = weu.event_id')
        ->join('wx_event_award award on award.id = weu.award_id' , 'left')
        ->join('mer_user user on user.id = weu.user_id ' , 'left')
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventUser->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventUser = M('WxEventUser');
    $ret = $WxEventUser->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventUser = M('WxEventUser');

    $ret = $WxEventUser->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventUser = M('WxEventUser');
    $ret = $WxEventUser->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function count_by_user_type_id( $user_id , $event_type , $event_id , $count_type = 'total') {
    $WxEventUser = M('WxEventUser');
    $field = ' count( user_id ) as amount';
    $where = array(
      'event_type' => $event_type,
      'event_id' => $event_id,
      'user_id' => $user_id,
    );
    if ( $count_type != 'today' ) {
      $today = date('Y-m-d');
      $where['create_time'][] = array('gt' , $today . ' 00:00:00');
      $where['create_time'][] = array('lt' , $today . ' 23:59:00');
    }
    
    return $WxEventUser->field( $field )->where($where)->group('user_id')->find();
  }
  
  function set_box_open( $id ) {
    $old_data = $this->get_by_id($id);
    $WxEventAward = WxEventAwardService::instance();
    $award_data = $WxEventAward->get_by_id($old_data['award_id']);
    
    if ( $award_data['valid_type'] == 1 ) {
      //如果是验证密码
      $data['award_status'] = 2 ;
    } else {
      //如果不是验证密码
      $data['award_status'] = 4 ;
      $data['award_valid_time'] = date('Y-m-d H:i:s') ;
    }
    
    $where = array(
      'id' => $id ,
      'award_status' => 1 ,
    );

    $WxEventUser = M('WxEventUser');
    $ret = $WxEventUser->where($where)->save( $data );
    //echo $WxEventUser->_sql();
    if ($ret) {
      return ajax_arr('查看成功', TRUE);
    } else {
      return ajax_arr('查看失败', FALSE);
    }
  }
  
  function set_award_auth( $id ) {
    $where = array(
      'id' => $id ,
      'award_status' => 2 ,
    );
    
    $data= array(
      'award_status' => 3 ,
      'award_valid_time' => date('Y-m-d H:i:s') ,
    );
    
    $WxEventUser = M('WxEventUser');
    $ret = $WxEventUser->where($where)->save( $data );
 
    if ($ret) {
      return ajax_arr('兑换成功', TRUE);
    } else {
      return ajax_arr('兑换失败，请稍后再试', FALSE);
    }
  }
}
